/*
ActualMasterRollupComputation
Version 1.0
Description: Trigger for computing fields prefixed 'RU' in master account from children account, calls rollUp() method 
             of MasterRollUp class which performs the bussiness logic.
Author: Pratibh Prakash
Created Date: 4 Mar-2009
*/
trigger ActualMasterRollupComputation on Account (after delete, before insert, after update) {
    Set<String> oldMasterAgencies = new Set<String>();
    Set<String> newMasterAgencies = new Set<String>();
    Set<String> oldMasterAgencies_filter = new Set<String>();
    Set<String> newMasterAgencies_filter = new Set<String>();
    List<String> filterFields = new List<String>();
    if (Trigger.isUpdate) {
        //either change in master agency or change in roll ups
      
        /*for (Account agNew: Trigger.new) {
            Account agOld = Trigger.oldMap.get(agNew.Id);
            if (agNew.Master_Agency_Code__c != agOld.Master_Agency_Code__c) {
                if ((!newMasterAgencies.contains(agNew.Master_Agency_Code__c)) && (agNew.Master_Agency_Code__c != null)) {
                    newMasterAgencies.add(agNew.Master_Agency_Code__c);
                }
         
                if ((!oldMasterAgencies.contains(agOld.Master_Agency_Code__c)) && (agOld.Master_Agency_Code__c != null)) {
                    oldMasterAgencies.add(agOld.Master_Agency_Code__c);
                }
            } 
            else if (agNew.PL_Actual_YTD__c != agOld.PL_Actual_YTD__c ||
                agNew.CL_Actual_YTD__c != agOld.CL_Actual_YTD__c ||
                agNew.PL_Rolling_12_WP__c != agOld.PL_Rolling_12_WP__c ||
                agNew.CL_Rolling_12_WP__c != agOld.CL_Rolling_12_WP__c ||
                agNew.PL_Actual_PY__c != agOld.PL_Actual_PY__c ||
                agNew.CL_Actual_PY__c != agOld.CL_Actual_PY__c ||
                agNew.CL_Variance_YTD__c != agOld.CL_Variance_YTD__c 
                ){
                    if ((!newMasterAgencies.contains(agNew.Master_Agency_Code__c)) && (agNew.Master_Agency_Code__c != null)) {
                        newMasterAgencies.add(agNew.Master_Agency_Code__c);
                    }
            }          
        }*/
        
        //Modification uses Filtering class FilterRecords.
        filterFields.add('Master_Agency_Code__c');
        FilterRecords obj = new FilterRecords(Trigger.old, Trigger.new,filterFields);
        Account[] newAcc = obj.getFilteredRecords_new();
        Account[] oldAcc = obj.getFilteredRecords_old();
        for(Account acc: newAcc){
             if ((!newMasterAgencies_filter.contains(acc.Master_Agency_Code__c)) && (acc.Master_Agency_Code__c != null)) {
                    newMasterAgencies_filter.add(acc.Master_Agency_Code__c);
                }
        }
        for(Account olds: oldAcc){
            if ((!oldMasterAgencies_filter.contains(olds.Master_Agency_Code__c)) && (olds.Master_Agency_Code__c != null)) {
                    oldMasterAgencies_filter.add(olds.Master_Agency_Code__c);
                }
        } 
        
        
        /*
        MasterRollUp.rollUp(newMasterAgencies);
        MasterRollUp.rollUp(oldMasterAgencies);*/
        
        //Performing rollup using sets() generated from FilterRecords class.
        MasterRollUp.rollUp(newMasterAgencies_filter);
        MasterRollUp.rollUp(oldMasterAgencies_filter);
    }
    
    else if (Trigger.isDelete){
        for (Account agOld: Trigger.old) {
            if ((!oldMasterAgencies.contains(agOld.Master_Agency_Code__c)) && (agOld.Master_Agency_Code__c != null)) {
                oldMasterAgencies.add(agOld.Master_Agency_Code__c);
            }         
        }
        MasterRollUp.rollUp(oldMasterAgencies);
    }
      
}